const codeStore = new Map();

const setCode = (email, code, ttl = 5 * 60 * 1000) => {
  codeStore.set(email, { code, expireAt: Date.now() + ttl, createdAt: Date.now() });
}

const getCode = (email) => {
  const data = codeStore.get(email);
  if (!data) return null;
  if (Date.now() > data.expireAt) {
    codeStore.delete(email);
    return null;
  }
  return data.code;
}

const getMailInfo = (email) => {
  const data = codeStore.get(email);
  if (!data) return null;
  if (Date.now() > data.expireAt) {
    codeStore.delete(email);
    return null;
  }
  return data;
}

const deleteCode = (email) => { codeStore.delete(email) }

const getAllMails = () => { return [...codeStore.values()] }

export { setCode, getCode, deleteCode, getMailInfo, getAllMails }
